﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;  
using log4net.Layout.Pattern;
using log4net.Layout;
using log4net.Core;
using System.Reflection;

namespace Runda.CMS.Common.LogComponent
{
    public class LogContent
    {
        public LogContent(int eventType, string eventCategory, int eventID, string computerName, string macAddress, string userName, string sourceType, string source, string description)
        {
            _event_Type = eventType;
            _eventCategory = eventCategory;
            _event_ID = eventID;
            _computerName = computerName;
            _mac_Address = macAddress;
            _userName = userName;
            _sourceType = sourceType;
            _source = source;
            _description = description;
        }


        int _event_Type;
        /// <summary>
        /// 时间类型 均为3
        /// </summary>
        public int Event_Type
        {
            get { return _event_Type; }
            set { _event_Type = value; }
        }
        string _eventCategory;
        /// <summary>
        /// 日志分类描述，自定义
        /// </summary>
        public string EventCategory
        {
            get { return _eventCategory; }
            set { _eventCategory = value; }
        }
        int _event_ID;
        /// <summary>
        /// 日志分类号
        /// </summary>
        public int Event_ID
        {
            get { return _event_ID; }
            set { _event_ID = value; }
        }
        string _computerName;
        /// <summary>
        /// 计算机IP
        /// </summary>
        public string ComputerName
        {
            get { return _computerName; }
            set { _computerName = value; }
        }
        string _mac_Address;
        /// <summary>
        /// 计算机Mac地址
        /// </summary>
        public string Mac_Address
        {
            get { return _mac_Address; }
            set { _mac_Address = value; }
        }
        string _userName;
        /// <summary>
        /// 系统登陆用户
        /// </summary>
        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }
        string _sourceType;
        /// <summary>
        /// Rier
        /// </summary>
        public string SourceType
        {
            get { return _sourceType; }
            set { _sourceType = value; }
        }
        string _source;
        /// <summary>
        /// Rier Recorder audit
        /// </summary>
        public string Source
        {
            get { return _source; }
            set { _source = value; }
        }
        string _description;
        /// <summary>
        /// 日志描述信息
        /// </summary>
        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }
    }

    public class MyLayout : PatternLayout
    {
        public MyLayout()
        {
            this.AddConverter("property", typeof(MyMessagePatternConverter));
        }
    }

    public class MyMessagePatternConverter : PatternLayoutConverter
    {
        protected override void Convert(System.IO.TextWriter writer, log4net.Core.LoggingEvent loggingEvent)
        {
            if (Option != null)
            {
                // Write the value for the specified key
                WriteObject(writer, loggingEvent.Repository, LookupProperty(Option, loggingEvent));
            }
            else
            {
                // Write all the key value pairs
                WriteDictionary(writer, loggingEvent.Repository, loggingEvent.GetProperties());
            }            
        }

        /// <summary>
        /// 通过反射获取传入的日志对象的某个属性的值
        /// </summary>
        /// <param name="property"></param>
        /// <returns></returns>
        private object LookupProperty(string property, log4net.Core.LoggingEvent loggingEvent)
        {
            object propertyValue = string.Empty;

            PropertyInfo propertyInfo = loggingEvent.MessageObject.GetType().GetProperty(property);
            if (propertyInfo != null)
                propertyValue = propertyInfo.GetValue(loggingEvent.MessageObject, null);

            return propertyValue;
        }
    }
}
